package com.gator.zuul.currentimiting;

import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimitUtils;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.RateLimitProperties;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.DefaultRateLimitKeyGenerator;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author PABLO
 * @Date 2022/5/23 17:03
 * @Desc 自定义限流
 */
@Component
public class CurrentLimiting extends DefaultRateLimitKeyGenerator {

    public CurrentLimiting(RateLimitProperties properties, RateLimitUtils rateLimitUtils) {
        super(properties, rateLimitUtils);
    }

    //将目标方法的id字段作为限流条件，这样设置只会对同样的id并且不是路径参数才生效
    //restful中携带的参数路径不生效，只要该请求路径都限流，不细分
    @Override
    public String key(HttpServletRequest request, Route route, RateLimitProperties.Policy policy) {
        return super.key(request, route, policy) + ":" + request.getParameter("id");
    }
}
